package com.qls.web.modules.dt.controller;

import com.qls.core.entity.DtOrderChannels;
import com.qls.core.utils.R;
import com.qls.web.common.utils.PageUtils;
import com.qls.web.modules.dt.service.DtOrderChannelsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 采购渠道
 *
 * @author yj
 * @email yj@gmail.com
 * @date 2021-09-11 14:01:21
 */
@RestController
@RequestMapping("dt/orderchannels")
public class DtOrderChannelsController {
    @Autowired
    private DtOrderChannelsService orderChannelsService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("dt:orderchannels:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = orderChannelsService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("dt:orderchannels:info")
    public R info(@PathVariable("id") Long id) {
        DtOrderChannels orderChannels = orderChannelsService.getById(id);
        return R.ok().put("orderChannels", orderChannels);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("dt:orderchannels:save")
    public R save(@RequestBody DtOrderChannels orderChannels) {
        Integer priority = orderChannels.getPriority();
        Integer count = orderChannelsService.getPriorityCount(priority);
        if (count > 0) {
            return R.error("优先级不能重复");
        }
        orderChannelsService.save(orderChannels);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("dt:orderchannels:update")
    public R update(@RequestBody DtOrderChannels orderChannels) {
        Integer priority = orderChannels.getPriority();
        DtOrderChannels dtOrderChannels = orderChannelsService.getById(orderChannels.getId());
        if (priority.equals(dtOrderChannels.getPriority())) {
            return R.ok();
        }
        Integer count = orderChannelsService.getPriorityCount(priority);
        if (count > 0) {
            return R.error("优先级不能重复");
        }
        orderChannelsService.updateById(orderChannels);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("dt:orderchannels:delete")
    public R delete(@RequestBody Long[] ids) {
        orderChannelsService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
